﻿using Admin2020.Core.Domain;
using Admin2020.Core.Domain.Core;
using Admin2020.Core.Helper;
using Admin2020.Data.Data;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Admin2020.Service.InitData
{
    public class DBInitializer
    {
        public async static Task InitDB(IServiceProvider service)
        {
            using (var scope = service.CreateScope())
            {
                //初始化，如果没有数据库，则创建数据库及数据表
                var db = scope.ServiceProvider.GetService<Admin2020DbContext>();
                db.Database.EnsureCreated();
                //db.Database.Migrate();

                //初始化系统数据
                var _rolesRepository = scope.ServiceProvider.GetService(typeof(IRepository<Roles>)) as EfRepository<Roles>;
                var _usersRepository = scope.ServiceProvider.GetService(typeof(IRepository<Users>)) as EfRepository<Users>;
                var _userRoleRepository = scope.ServiceProvider.GetService(typeof(IRepository<UserRole>)) as EfRepository<UserRole>;
                var _rolePermissionRepository = scope.ServiceProvider.GetService(typeof(IRepository<RolePermission>)) as EfRepository<RolePermission>;

                //var hasUsers = _usersRepository.Table.Count() > 0;

                if (!_usersRepository.Table.Any())
                {

                    //角色
                    var role = new Roles { RoleName = "系统管理员", Description = "系统管理员", DisplayOrder = 0 };
                    //var roleList = new List<Roles>
                    //{
                    //    new Roles{RoleName="系统管理员",Description="系统管理员",DisplayOrder=0},
                    //    new Roles{RoleName="供应商",Description="供应商",DisplayOrder=0},
                    //    new Roles{RoleName="门店",Description="门店",DisplayOrder=0},
                    //};
                    await _rolesRepository.InsertAsync(role);

                    //用户
                    var defaultPassoword = "admin";
                    var salt = CryptoHelper.GetRandomSalt();
                    var password = CryptoHelper.HashPassword(defaultPassoword, salt);
                    var user = new Users { UserName = "admin", Salt = salt, PassWord = password, NickName = "管理员", AvatarUrl = @"images\uploaded\1e760121585843a1b1875c8cc21565fb.png", Email = "abc@abc.com", Telephone = "13677889900", CurrentStatus = 1 };
                    _usersRepository.Insert(user);

                    //用户角色
                    var userRole = new UserRole { UsersId = user.Id, RolesId = _rolesRepository.Table.First(x => x.RoleName == "系统管理员").Id };
                    _userRoleRepository.Insert(userRole);

                    //角色权限
                    var rolePermission = new List<RolePermission>
                    {
                        new RolePermission{RolesId=role.Id,AccessName="system-setting",AccessTitle="系统设置",DisplayOrder=0},
                        new RolePermission{RolesId=role.Id,AccessName="role",AccessTitle="角色",DisplayOrder=0},
                        new RolePermission{RolesId=role.Id,AccessName="user",AccessTitle="用户",DisplayOrder=0},
                    };
                    _rolePermissionRepository.Insert(rolePermission);
                }
            }
        }
    }
}
